/**
 * 亿牛集团
 * 本源代码由亿牛集团及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2017 yiniu.com all rights reserved.
 */

package com.rico.simplejdbc.jdbc06.web;

import com.rico.simplejdbc.jdbc06.sdkDrive.entities.SdkDriveGame;
import com.rico.simplejdbc.jdbc06.sdkDrive.dao.SdkDriveGameDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yaoyongfeng
 */
@Controller
@RequestMapping("/game")
public class SdkDriveGameController {
    @Autowired
    SdkDriveGameDao sdkDriveGameDao;

    @GetMapping(path = "/lists")
    @ResponseBody
    public List<SdkDriveGame> lists() {
        List<SdkDriveGame> all = sdkDriveGameDao.findAll();
        return all;
    }


    @GetMapping(path = "/view/{id}")
    @ResponseBody
    public SdkDriveGame view(@PathVariable Integer id) {
        SdkDriveGame sdkDriveGame = sdkDriveGameDao.findById(id).orElse(null);
        return sdkDriveGame;
    }


    @GetMapping(path = "/save")
    @ResponseBody
    public SdkDriveGame save() {
        SdkDriveGame sdkDriveGame = new SdkDriveGame();
        sdkDriveGame.setName("测试jpa游戏");
        sdkDriveGame.setCode("testjpa");
        sdkDriveGame.setPackname("jpa package");
        sdkDriveGame.setType(1);
        sdkDriveGame.setIcon("");
        sdkDriveGame.setVersion("");
        sdkDriveGame.setSize("");
        sdkDriveGame.setIntro("");
        sdkDriveGame.setAddtime((int) (System.currentTimeMillis() / 1000));
        sdkDriveGame.setNum(1);
        sdkDriveGame.setCallback("");
        sdkDriveGame.setSecret("");
        sdkDriveGame.setRyappkey("");
        sdkDriveGame.setStatus(1);

        SdkDriveGame save = sdkDriveGameDao.save(sdkDriveGame);
        return save;
    }

    @GetMapping(path = "/delete/{id}")
    @ResponseBody
    public String remove(@PathVariable Integer id) {
        SdkDriveGame sdkDriveGame = sdkDriveGameDao.findById(id).orElse(null);
        if (sdkDriveGame.equals(null)) {
            return "游戏不存在";
        }
        sdkDriveGameDao.delete(sdkDriveGame);
        return "删除成功";
    }

    @GetMapping(path = "/update/{id}")
    @ResponseBody
    public String update(@PathVariable Integer id) {
        SdkDriveGame sdkDriveGame = sdkDriveGameDao.findById(id).orElse(null);
        if (sdkDriveGame.equals(null)) {
            return "游戏不存在";
        }
        sdkDriveGame.setName("已经修改");
        sdkDriveGameDao.saveAndFlush(sdkDriveGame);
        return "修改修改成功";
    }

}
